<template>
    <div class="city-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="城市名称" prop="name">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.name"
                        clearable
                        @keyup.enter="getLists"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getLists">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <el-button type="primary" @click="handleAdd()">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增城市
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table
                ref="tableRef"
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column label="ID" prop="id" width="80" />
                <el-table-column label="城市名称" prop="name" min-width="120" />
                <el-table-column label="拼音" prop="pinyin" min-width="120" />
                <el-table-column label="行政区划" prop="area_code" min-width="120" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button type="primary" link @click="handleEdit(row)">
                            编辑
                        </el-button>
                        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="city">
import type { ElTable, FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import feedback from '@/utils/feedback'
import { usePaging } from '@/hooks/usePaging'
import { cityLists } from '@/api/supervise/city'
import EditPopup from './edit.vue'

const tableRef = shallowRef<InstanceType<typeof ElTable>>()
const formRef = shallowRef<FormInstance>()

const queryParams = reactive({
    name: ''
})

// 使用 usePaging hook
const { pager, getLists,resetParams } = usePaging({
    fetchFun: cityLists,
    params: queryParams
})

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (row: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(row)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除该城市？')
    // 实现删除逻辑
}

onMounted(() => {
    getLists()
})
</script>

<style scoped lang="scss">
.city-lists {
    :deep(.el-table__inner-wrapper::before) {
        background-color: transparent;
    }
}
</style>